using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;
using System.IO;

namespace C4F.DevKit.P2PPictureShare.PictureShareWpf
{
    // NotifyIcon and related components & controls for creating the visual
    // part of a tray application are contained in a user control. The reason
    // for this is to allow WPF to host the NotifyIcon component -- WPF does
    // not offer a similar component at this time.
    public sealed class NotifyIconControl : UserControl
    {
        // Container to store our Windows Forms components.
        IContainer _components = null;
        
        // Our NotifyIcon component (sits in tray next to clock and shows icon).
        NotifyIcon _trayIcon;
        
        // Our context menu to display when user right-clicks on tray icon.
        ContextMenu _trayMenu;
        // Menu items for context menu representing available options.
        MenuItem _appShowMenuItem;
        MenuItem _appOpenOutFolder;
        MenuItem _appOpenInFolder;
        MenuItem _appExitMenuItem;
        MenuItem _appPublishMenuItem;

        // Events for each context menu item choice.
        public event EventHandler<EventArgs> ShowConfigWindowSelected;
        public event EventHandler<EventArgs> PublishSelected;
        public event EventHandler<EventArgs> ExitSelected;
        public event EventHandler<EventArgs> OpenInFolder;
        public event EventHandler<EventArgs> OpenOutFolder;
        

        public NotifyIconControl()
        {
            // Initialize UI components.
            InitializeContext();
        }

        private void InitializeContext()
        {
            // Instantiate components.
            this._components = new Container();
            this._trayIcon = new NotifyIcon(this._components);
            this._trayMenu = new ContextMenu();
            this._appShowMenuItem = new MenuItem();
            this._appPublishMenuItem = new MenuItem();
            this._appOpenOutFolder = new MenuItem();
            this._appOpenInFolder = new MenuItem();
            this._appExitMenuItem = new MenuItem();

            int menuIndex = 0;

            // Setup the context menu items.
            this._appPublishMenuItem.Index = menuIndex++;
            this._appPublishMenuItem.Text = "Publish outgoing pictures";
            this._appPublishMenuItem.DefaultItem = true;
            this._appPublishMenuItem.Click += new EventHandler(appPublishMenuItem_Click);

            this._appOpenOutFolder.Index = menuIndex++;
            this._appOpenOutFolder.Text = "Open outgoing picture folder";
            this._appOpenOutFolder.Click += new EventHandler(appOpenOutFolder_Click);

            this._appOpenInFolder.Index = menuIndex++;
            this._appOpenInFolder.Text = "Open incoming picture folder";
            this._appOpenInFolder.Click += new EventHandler(appOpenInFolder_Click);

            MenuItem sep1 = new MenuItem();
            sep1.Index = 1;
            sep1.Text = "-";

            this._appShowMenuItem.Index = menuIndex++;
            this._appShowMenuItem.Text = "Configuration";
            this._appShowMenuItem.Click += new EventHandler(appShowMenuItem_Click);

            MenuItem sep2 = new MenuItem();
            sep2.Index = 3;
            sep2.Text = "-";

            this._appExitMenuItem.Index = menuIndex++;
            this._appExitMenuItem.Text = "Exit";
            this._appExitMenuItem.Click += new EventHandler(appExitMenuItem_Click);

            // Add context menu items to the context menu.
            this._trayMenu.MenuItems.AddRange(new MenuItem[] { _appPublishMenuItem, _appOpenOutFolder, _appOpenInFolder, sep1, _appShowMenuItem, sep2, _appExitMenuItem });

            // Assign context menu to NotifyIcon.
            this._trayIcon.ContextMenu = this._trayMenu;
            
            // Finish NotifyIcon setup.
            this._trayIcon.Icon = Properties.Resources.TrayIcon;
            this._trayIcon.Visible = true;
            this._trayIcon.DoubleClick += new EventHandler(trayIcon_DoubleClick);
        }


        // Expose the Text propery of the NotifyIcon (shown as tooltip during mouseover).
        public string TrayText
        {
            get
            { return this._trayIcon.Text; }

            set
            {
                if (value == null)
                { throw new ArgumentNullException("value"); }

                this._trayIcon.Text = value;
            }
        }

        // Called when user clicks on appShowMenuItem context menu item.
        void appShowMenuItem_Click(object sender, EventArgs e)
        {
            EventHandler<EventArgs> currentEvent = this.ShowConfigWindowSelected;

            if (currentEvent != null)
            {
                currentEvent(sender, e);
            }
        }

        // Called when user double-clicks on appShowMenuItem context menu item.
        void trayIcon_DoubleClick(object sender, EventArgs e)
        {
            EventHandler<EventArgs> currentEvent = this.ShowConfigWindowSelected;
            if (currentEvent != null)
            {
                currentEvent(sender, e);
            }
        }

        // Called when user clicks on appPublishMenuItem context menu item.
        void appPublishMenuItem_Click(object sender, EventArgs e)
        {
            EventHandler<EventArgs> currentEvent = this.PublishSelected;

            if (currentEvent!= null)
            {
                currentEvent(sender, e);
            }
        }

        void appOpenInFolder_Click(object sender, EventArgs e)
        {
            EventHandler<EventArgs> currentEvent = this.OpenInFolder;

            if (currentEvent != null)
            {
                currentEvent(sender, e);
            }
        }

        void appOpenOutFolder_Click(object sender, EventArgs e)
        {
            EventHandler<EventArgs> currentEvent = this.OpenOutFolder;

            if (currentEvent != null)
            {
                currentEvent(sender, e);
            }
        }

        // Called when user clicks on appExitMenuItem context menu item.
        void appExitMenuItem_Click(object sender, EventArgs e)
        {
            EventHandler<EventArgs> currentEvent = this.ExitSelected;

            if (currentEvent != null)
            {
                currentEvent(sender, e);
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (_components != null)
                {
                    _components.Dispose();
                }
            }

            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.SuspendLayout();
            this.Name = "NotifyIconControl";
            this.ResumeLayout(false);
        }
    }
}
